package com.mzj.saas.jobs.bill.repository;


import com.mzj.saas.jobs.bill.entity.AppUser;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface AppUserRepository
        extends PagingAndSortingRepository<AppUser, String>, JpaSpecificationExecutor<AppUser> {
    @Query("from AppUser where phone=:phone and status = 1")
    AppUser findByPhone(@Param("phone") String phone);

    @Query("from AppUser where openId=:openId and status = 1")
    AppUser findByOpenId(@Param("openId") String openId);

    @Query("from AppUser where iosOpenId=:iosOpenId and status = 1")
    AppUser findByIosOpenId(@Param("iosOpenId") String iosOpenId);

    @Query(value = "select * from saas_app_user where (id = ?1 or enterprise_user_id = ?2) and status = 1", nativeQuery = true)
    AppUser findByIdOrEnterpriseUserId(String id, String enterpriseUserId);

    /**
     * 根据普通用户id查询
     */
    AppUser findByIdAndStatus(String id, Integer status);


    AppUser findByCertifyId(String certifyId );


    /**
     * 根据企业用户id查询
     */
    AppUser findByEnterpriseUserIdAndStatus(String id, Integer status);

    /**
     * 根据企业用户id查询
     */
    List<AppUser> findByEnterpriseUserId(String id);

    /**
     * 批量查询用户, 根据普通用户id与企业用户id
     */
    @Query(value = "select * from saas_app_user where ( id in(?1) or enterprise_user_id in (?2) ) and status = 1", nativeQuery = true)
    List<AppUser> findByIdOrEnterpriseIn(List<String> ids1, List<String> ids2);

    @Query(value = "select * from saas_app_user where card_number = ? and status = 1", nativeQuery = true)
    AppUser findByCardNumber(String cardNumber);

    List<AppUser> findByNameLike(String name);

    List<AppUser> findByEnterpriseUserIdIn(List<String> enterpriseUserIds);

    List<AppUser> findByStatus(Integer status);

    List<AppUser> findByStatusAndCertificationStatus(Integer status,Integer certificationStatus);

}
